package com.openbravo.possync;

import com.openbravo.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.ImageUtils;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerRead;
import com.openbravo.data.loader.SerializerReadClass;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.SerializerWriteString;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.SessionDB;
import com.openbravo.data.loader.StaticSentence;
import com.openbravo.data.loader.Transaction;
import com.openbravo.pos.customers.CustomerInfoExt;
import com.openbravo.pos.forms.BeanFactoryDataSingle;
import com.openbravo.pos.inventory.TaxCategoryInfo;
import com.openbravo.pos.payment.PaymentInfoTicket;
import com.openbravo.pos.ticket.CategoryInfo;
import com.openbravo.pos.ticket.ProductInfoExt;
import com.openbravo.pos.ticket.TaxInfo;
import com.openbravo.pos.ticket.TicketInfo;
import com.openbravo.pos.ticket.TicketLineInfo;
import java.util.List;

public class DataLogicIntegration extends BeanFactoryDataSingle
{
  protected Session s;

  public void init(Session paramSession)
  {
    this.s = paramSession;
  }

  public void syncCustomersBefore()
    throws BasicException
  {
    new StaticSentence(this.s, "UPDATE CUSTOMERS SET VISIBLE = " + this.s.DB.FALSE()).exec();
  }

  public void syncCustomer(final CustomerInfoExt paramCustomerInfoExt)
    throws BasicException
  {
      Transaction local1 = new Transaction(this.s )
    {
      public Object transact()
        throws BasicException
      {
        if (new PreparedSentence(DataLogicIntegration.this.s, "UPDATE CUSTOMERS SET SEARCHKEY = ?, NAME = ?, NOTES = ?, VISIBLE = " + DataLogicIntegration.this.s.DB.TRUE() + " WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
        {
          public void writeValues()
            throws BasicException
          {
            setString(1, paramCustomerInfoExt.getSearchkey());
            setString(2, paramCustomerInfoExt.getName());
            setString(3, paramCustomerInfoExt.getAddress());
            setString(4, paramCustomerInfoExt.getId());
          }
        }) == 0)
          new PreparedSentence(DataLogicIntegration.this.s, "INSERT INTO CUSTOMERS(ID, SEARCHKEY, NAME, NOTES, VISIBLE) VALUES (?, ?, ?, ?, " + DataLogicIntegration.this.s.DB.TRUE() + ")", SerializerWriteParams.INSTANCE).exec(new DataParams()
          {
            public void writeValues()
              throws BasicException
            {
              setString(1, paramCustomerInfoExt.getId());
              setString(2, paramCustomerInfoExt.getSearchkey());
              setString(3, paramCustomerInfoExt.getName());
              setString(4, paramCustomerInfoExt.getAddress());
            }
          });
        return null;
      }
    };
    local1.execute();
  }

  public void syncProductsBefore()
    throws BasicException
  {
    new StaticSentence(this.s, "DELETE FROM PRODUCTS_CAT").exec();
  }

  public void syncTaxCategory(final TaxCategoryInfo paramTaxCategoryInfo)
    throws BasicException
  {
	  Transaction  transaction = new Transaction(this.s )
    {
      public Object transact()
        throws BasicException
      {
        if (new PreparedSentence(DataLogicIntegration.this.s, "UPDATE TAXCATEGORIES SET NAME = ?  WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
        {
          public void writeValues()
            throws BasicException
          {
            setString(1, paramTaxCategoryInfo.getName());
            setString(2, paramTaxCategoryInfo.getID());
          }
        }) == 0)
          new PreparedSentence(DataLogicIntegration.this.s, "INSERT INTO TAXCATEGORIES(ID, NAME) VALUES (?, ?)", SerializerWriteParams.INSTANCE).exec(new DataParams()
          {
            public void writeValues()
              throws BasicException
            {
              setString(1, paramTaxCategoryInfo.getID());
              setString(2, paramTaxCategoryInfo.getName());
            }
          });
        return null;
      }
    };
    transaction.execute();
  }

  public void syncTax(final TaxInfo paramTaxInfo)
    throws BasicException
  {
      Transaction local3 = new Transaction(this.s )
    {
      public Object transact()
        throws BasicException
      {
        if (new PreparedSentence(DataLogicIntegration.this.s, "UPDATE TAXES SET NAME = ?, CATEGORY = ?, CUSTCATEGORY = ?, PARENTID = ?, RATE = ?, RATECASCADE = ? WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
        {
          public void writeValues()
            throws BasicException
          {
            setString(1, paramTaxInfo.getName());
            setString(2, paramTaxInfo.getTaxCategoryID());
            setString(3, paramTaxInfo.getTaxCustCategoryID());
            setString(4, paramTaxInfo.getParentID());
            setDouble(5, Double.valueOf(paramTaxInfo.getRate()));
            setBoolean(6, Boolean.valueOf(paramTaxInfo.isCascade()));
            setString(7, paramTaxInfo.getId());
          }
        }) == 0)
          new PreparedSentence(DataLogicIntegration.this.s, "INSERT INTO TAXES(ID, NAME, CATEGORY, CUSTCATEGORY, PARENTID, RATE, RATECASCADE) VALUES (?, ?, ?, ?, ?, ?, ?)", SerializerWriteParams.INSTANCE).exec(new DataParams()
          {
            public void writeValues()
              throws BasicException
            {
              setString(1, paramTaxInfo.getId());
              setString(2, paramTaxInfo.getName());
              setString(3, paramTaxInfo.getTaxCategoryID());
              setString(4, paramTaxInfo.getTaxCustCategoryID());
              setString(5,paramTaxInfo.getParentID());
              setDouble(6, Double.valueOf(paramTaxInfo.getRate()));
              setBoolean(7, Boolean.valueOf(paramTaxInfo.isCascade()));
            }
          });
        return null;
      }
    };
    local3.execute();
  }

  public void syncCategory(final CategoryInfo paramCategoryInfo)
    throws BasicException
  {
	  Transaction  local4 = new Transaction(this.s )
    {
      public Object transact()
        throws BasicException
      {
        if (new PreparedSentence(DataLogicIntegration.this.s, "UPDATE CATEGORIES SET NAME = ?, IMAGE = ? WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
        {
          public void writeValues()
            throws BasicException
          {
            setString(1, paramCategoryInfo.getName());
            setBytes(2, ImageUtils.writeImage(paramCategoryInfo.getImage()));
            setString(3, paramCategoryInfo.getID());
          }
        }) == 0)
          new PreparedSentence(DataLogicIntegration.this.s, "INSERT INTO CATEGORIES(ID, NAME, IMAGE) VALUES (?, ?, ?)", SerializerWriteParams.INSTANCE).exec(new DataParams()
          {
            public void writeValues()
              throws BasicException
            {
              setString(1, paramCategoryInfo.getID());
              setString(2, paramCategoryInfo.getName());
              setBytes(3, ImageUtils.writeImage(paramCategoryInfo.getImage()));
            }
          });
        return null;
      }
    };
    local4.execute();
  }

  public void syncProduct(final ProductInfoExt paramProductInfoExt)
    throws BasicException
  {
	  Transaction local5 = new Transaction(this.s )
    {
      public Object transact()
        throws BasicException
      {
        if (new PreparedSentence(DataLogicIntegration.this.s, "UPDATE PRODUCTS SET REFERENCE = ?, CODE = ?, NAME = ?, PRICEBUY = ?, PRICESELL = ?, CATEGORY = ?, TAXCAT = ?, IMAGE = ? WHERE ID = ?", SerializerWriteParams.INSTANCE).exec(new DataParams()
        {
          public void writeValues()
            throws BasicException
          {
            setString(1, paramProductInfoExt.getReference());
            setString(2,paramProductInfoExt.getCode());
            setString(3, paramProductInfoExt.getName());
            setDouble(4, Double.valueOf(paramProductInfoExt.getPriceBuy()));
            setDouble(5, Double.valueOf(paramProductInfoExt.getPriceSell()));
            setString(6, paramProductInfoExt.getCategoryID());
            setString(7, paramProductInfoExt.getTaxCategoryID());
            setBytes(8, ImageUtils.writeImage(paramProductInfoExt.getImage()));
            setString(9, paramProductInfoExt.getID());
          }
        }) == 0)
          new PreparedSentence(DataLogicIntegration.this.s, "INSERT INTO PRODUCTS (ID, REFERENCE, CODE, NAME, ISCOM, ISSCALE, PRICEBUY, PRICESELL, CATEGORY, TAXCAT, IMAGE, STOCKCOST, STOCKVOLUME) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", SerializerWriteParams.INSTANCE).exec(new DataParams()
          {
            public void writeValues()
              throws BasicException
            {
              setString(1, paramProductInfoExt.getID());
              setString(2, paramProductInfoExt.getReference());
              setString(3, paramProductInfoExt.getCode());
              setString(4, paramProductInfoExt.getName());
              setBoolean(5, Boolean.valueOf(paramProductInfoExt.isCom()));
              setBoolean(6, Boolean.valueOf(paramProductInfoExt.isScale()));
              setDouble(7, Double.valueOf(paramProductInfoExt.getPriceBuy()));
              setDouble(8, Double.valueOf(paramProductInfoExt.getPriceSell()));
              setString(9, paramProductInfoExt.getCategoryID());
              setString(10, paramProductInfoExt.getTaxCategoryID());
              setBytes(11, ImageUtils.writeImage(paramProductInfoExt.getImage()));
              setDouble(12, Double.valueOf(0.0D));
              setDouble(13, Double.valueOf(0.0D));
            }
          });
        new StaticSentence(DataLogicIntegration.this.s, "INSERT INTO PRODUCTS_CAT(PRODUCT, CATORDER) VALUES (?, NULL)", SerializerWriteString.INSTANCE).exec(paramProductInfoExt.getID());
        return null;
      }
    };
    local5.execute();
  }

  public List getTickets()
    throws BasicException
  {
    return new PreparedSentence(this.s, "SELECT T.ID, T.TICKETTYPE, T.TICKETID, R.DATENEW, R.MONEY, P.ID, P.NAME, C.ID, C.TAXID, C.SEARCHKEY, C.NAME FROM RECEIPTS R JOIN TICKETS T ON R.ID = T.ID LEFT OUTER JOIN PEOPLE P ON T.PERSON = P.ID LEFT OUTER JOIN CUSTOMERS C ON T.CUSTOMER = C.ID WHERE (T.TICKETTYPE = 0 OR T.TICKETTYPE = 1) AND T.STATUS = 0", null, new SerializerReadClass(TicketInfo.class)).list();
  }

  public List getTicketLines(String paramString)
    throws BasicException
  {
    return new PreparedSentence(this.s, "SELECT L.TICKET, L.LINE, L.PRODUCT, L.UNITS, L.PRICE, T.ID, T.NAME, T.CATEGORY, T.CUSTCATEGORY, T.PARENTID, T.RATE, T.RATECASCADE, T.RATEORDER, L.ATTRIBUTES FROM TICKETLINES L, TAXES T WHERE L.TAXID = T.ID AND L.TICKET = ?", SerializerWriteString.INSTANCE, new SerializerReadClass(TicketLineInfo.class)).list(paramString);
  }

  public List getTicketPayments(String paramString)
    throws BasicException
  {
    return new PreparedSentence(this.s, "SELECT TOTAL, PAYMENT FROM PAYMENTS WHERE RECEIPT = ?", SerializerWriteString.INSTANCE, new SerializerRead()
    {
      public Object readValues(DataRead paramDataRead)
        throws BasicException
      {
        return new PaymentInfoTicket(paramDataRead.getDouble(1).doubleValue(), paramDataRead.getString(2));
      }
    }).list(paramString);
  }

  public void execTicketUpdate()
    throws BasicException
  {
    new StaticSentence(this.s, "UPDATE TICKETS SET STATUS = 1 WHERE STATUS = 0").exec();
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.possync.DataLogicIntegration
 * JD-Core Version:    0.6.0
 */